package com.example.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {

    private static final String KEY = "GXMZU-KUNPENG";
    private static final long EXPIRE_TIME = 1000 * 60 * 60;

    /**
     * 接收业务数据,生成token并返回
     * @param claims
     * @return
     */
    public static String genToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("claims", claims)//业务数据
                .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRE_TIME))//有效期
                .sign(Algorithm.HMAC384(KEY));//密钥
    }

    /**
     * 解析token，返回业务数据
     * @param token
     * @return
     */
    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC384(KEY))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }



}
